package sfin.swt.composite;

import java.util.ResourceBundle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import sfin.controller.ContaController;
import sfin.swt.exception.StatizoException;
import sfin.swt.util.MessageUtil;

public class ContaCorrenteComposite extends Composite{
	
	private ResourceBundle bundle;
	private ContaController controller;
	
	private Text txtNome;
	private Text txtAgencia;
	private Text txtConta;
	private Combo cbBanco;

	private Table table;
	private Button delButton;
	private MessageUtil messageUtil;
	private Button chkDefault;
	
	public ContaCorrenteComposite(Composite parent, ResourceBundle bundle, ContaController controller, MessageUtil messageUtil, int style) {
		super(parent, style);
		this.bundle = bundle;
		this.controller = controller;
		this.messageUtil = messageUtil;
		initialize();
		fillData(true);
	}
	
	private void fillData(){
		fillData(false);
	}
	
	private void fillData(boolean initial){
		
		table.removeAll();
		String[][] data = controller.getContas();
		for (int i = 0; i < data.length; i++) {
			TableItem item = new TableItem(table, SWT.NONE);
			item.setText(data[i]);
		}

		cbBanco.setItems(controller.getBancos());
		cbBanco.select(controller.getBanco());
		chkDefault.setSelection(controller.isPadrao());
		
		if(!initial){
			txtNome.setText(controller.getApelido());
			txtAgencia.setText(controller.getAgencia());
			txtConta.setText(controller.getConta());

			table.select(controller.getSelectedIndex());
		}

	}

	private void initialize() {

		GridLayout layout = new GridLayout(2,true);
		setLayout(layout);
		
		GridData gridData = new GridData();
		
		Label label = new Label(this,SWT.NONE);
		label.setText(bundle.getString("current.account.edit.label.name"));
		label.setLayoutData(gridData);

		gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.widthHint = 200;
		txtNome = new Text(this,SWT.BORDER);
		txtNome.setLayoutData(gridData);

		gridData = new GridData(GridData.FILL_HORIZONTAL);
		label = new Label(this,SWT.NONE);
		label.setText(bundle.getString("current.account.edit.label.agency"));
		label.setLayoutData(gridData);

		gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.widthHint = 100;
		txtAgencia = new Text(this,SWT.BORDER);
		txtAgencia.setLayoutData(gridData);
		
		gridData = new GridData();
		label = new Label(this,SWT.NONE);
		label.setText(bundle.getString("current.account.edit.label.number"));
		label.setLayoutData(gridData);

		txtConta = new Text(this,SWT.BORDER);

		gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.widthHint = 100;
		txtConta.setLayoutData(gridData);
		
		label = new Label(this,SWT.NONE);
		label.setText(bundle.getString("current.account.edit.label.bank"));
		
		gridData = new GridData();
		label.setLayoutData(gridData);

		gridData = new GridData();
		cbBanco = new Combo(this,SWT.BORDER | SWT.READ_ONLY);
		cbBanco.setLayoutData(gridData);

		label = new Label(this,SWT.NONE);
		label.setText(bundle.getString("current.account.edit.label.default"));
		
		gridData = new GridData();
		label.setLayoutData(gridData);
		
		chkDefault = new Button(this, SWT.CHECK);
		
		gridData = new GridData();
		chkDefault.setLayoutData(gridData);
		
		Group group = new Group(this,SWT.NONE);
		group.setText(bundle.getString("categ.edit.table.group.title"));
		group.setLayout(new GridLayout());
		gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = 2;
		gridData.verticalSpan = 10;
		gridData.heightHint = 350;
		group.setLayoutData(gridData);
	
		table = new Table(group,SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);
		table.setLinesVisible(true);
		table.setHeaderVisible(true);

		TableColumn column = new TableColumn(table, SWT.BORDER);
		column.setText(bundle.getString("current.account.table.column.name"));
		column.setWidth(220);

		column = new TableColumn(table, SWT.NONE);
		column.setText(bundle.getString("current.account.table.column.agency"));
		column.setWidth(100);
		
		column = new TableColumn(table, SWT.NONE);
		column.setText(bundle.getString("current.account.table.column.number"));
		column.setWidth(100);

		column = new TableColumn(table, SWT.NONE);
		column.setText(bundle.getString("current.account.table.column.bank"));
		column.setWidth(100);
		
		column = new TableColumn(table, SWT.NONE);
		column.setText(bundle.getString("current.account.edit.label.default"));
		column.setWidth(60);

		table.setLayoutData(new GridData(GridData.FILL_VERTICAL | GridData.FILL_HORIZONTAL));
		table.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				controller.setSelectedIndex(table.getSelectionIndex());
				fillData();
				delButton.setEnabled(true);
			}
		});
		
		label = new Label(this,SWT.SEPARATOR | SWT.HORIZONTAL);
		gridData  = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = 2;
		label.setLayoutData(gridData);
		
		Composite actionComposite = new Composite(this,SWT.NONE);
		actionComposite.setLayout(new RowLayout(SWT.HORIZONTAL));
		gridData = new GridData();
		gridData.horizontalSpan = 2;
		actionComposite.setLayoutData(gridData);
		
		Button button = new Button(actionComposite,SWT.PUSH);
		button.setText(bundle.getString("string.general.save"));
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				controller.setApelido(txtNome.getText());
				controller.setAgencia(txtAgencia.getText());
				controller.setConta(txtConta.getText());
				controller.setBanco(cbBanco.getSelectionIndex());
				controller.setPadrao(chkDefault.getSelection());
				
				try {
					controller.callSave();
					fillData();
				} catch (StatizoException e1) {
					messageUtil.showMessage(e1);
				}
			}
		});
		
		button = new Button(actionComposite,SWT.PUSH);
		button.setText(bundle.getString("bank.edit.action.add"));
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				controller.clearSelection();
				fillData();
				delButton.setEnabled(false);
			}
		});
		
		delButton = new Button(actionComposite,SWT.PUSH);
		delButton.setText(bundle.getString("string.general.delete"));
		delButton.setEnabled(false);
		delButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				try {
					controller.callDel();
					fillData();
					if(!controller.existConta())
						delButton.setEnabled(false);
				} catch (StatizoException e1) {
					messageUtil.showMessage(e1);
				}
			}
		});
	}

}
